package transactionV1

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"hc_server/pkg/app"
	"hc_server/pkg/channel"
	"hc_server/routers/service/transactionService"
	"hc_server/runtime/e"
)

type PayParam struct {
	MerchantId interface{} `json:"merchantId"` //商户号
	Type       interface{} `json:"type"`       //1-微信；2-支付宝；3-云闪付；5-聚分期；
	Amount     interface{} `json:"amount"`     //交易金额，单位为元，保留两位小数，取值范围[0.01,100000000]
	Openid     interface{} `json:"openid"`     //微信、支付宝、云闪付的id
	Subject    interface{} `json:"subject"`    //订单标题
	Body       interface{} `json:"body"`       //商品描述
	DeviceId   interface{} `json:"deviceId"`   //交易设备id
}

// LianPay 用户扫码交易--联动
func LianPay(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   PayParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	transaction := transactionService.Transaction{
		MerchantId: cast.ToInt(param.MerchantId),
		Type:       cast.ToInt(param.Type),
		Amount:     cast.ToFloat32(param.Amount),
		Openid:     cast.ToString(param.Openid),
		Subject:    cast.ToString(param.Subject),
		Body:       cast.ToString(param.Body),
		DeviceId:   cast.ToInt(param.DeviceId),
	}

	result, err := transaction.TransactionPay()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type PayCodeParam struct {
	MerchantId interface{} `json:"merchantId"` //商户号
	Type       interface{} `json:"type"`       //1-微信；2-支付宝；3-云闪付；
	Amount     interface{} `json:"amount"`     //交易金额，单位为元，保留两位小数，取值范围[0.01,100000000]
	AuthCode   interface{} `json:"authCode"`   //客户付款码明文
	Subject    interface{} `json:"subject"`    //订单标题
	Body       interface{} `json:"body"`       //商品描述
	DeviceId   interface{} `json:"deviceId"`   //交易设备id
}

// LianPayCode 用户展示付款码交易--联动
func LianPayCode(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   PayCodeParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	transaction := transactionService.Transaction{
		MerchantId: cast.ToInt(param.MerchantId),
		Type:       cast.ToInt(param.Type),
		Amount:     cast.ToFloat32(param.Amount),
		AuthCode:   cast.ToString(param.AuthCode),
		Subject:    cast.ToString(param.Subject),
		Body:       cast.ToString(param.Body),
		DeviceId:   cast.ToInt(param.DeviceId),
	}

	result, err := transaction.TransactionPayCode()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type PayOrderListParam struct {
	MerchantId interface{} `json:"merchantId"` //商户号
}

// LianOrderList 订单查询
func LianOrderList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   PayOrderListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	transaction := transactionService.Transaction{
		MerchantId: cast.ToInt(param.MerchantId),
	}

	result, err := transaction.TransactionLianOrderList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

// LianNotifyUrl 订单查询
func LianNotifyUrl(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   channel.NotifyUrlParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	transaction := transactionService.Transaction{}

	err := transaction.TransactionLianNotifyUrl(&param)
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", nil)
}

type LianNotifyUrlParam struct {
	CallBack   string `json:"call_back"` //商户号
	PayOrderNo string `json:"pay_order_no"`
}

// LianNotifyUrl1 订单查询
func LianNotifyUrl1(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   LianNotifyUrlParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	transaction := transactionService.Transaction{}

	err := transaction.TransactionLianNotifyUrl1(cast.ToString(param.CallBack), cast.ToString(param.PayOrderNo))
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", nil)
}
